import request from '../utils/request'
import { getToken } from '@/utils/token.js'
import { getStorage } from '../utils/storage'
import dayjs from 'dayjs'
// 获取所有频道
export const getAllChannelsAPI = () => {
  return request({
    url: '/v1_0/channels',
    method: 'GET'
  })
}

// 登录接口

export const loginAPI = ({ mobile, code }) => request({
  url: '/v1_0/authorizations',
  method: 'POST',
  data: {
    mobile,
    code
  }
})

// 用户选择的请求
export const getUserChannelsAPI = () => request({
  url: '/v1_0/user/channels',
  headers: {
    Authorization: `Bearer ${getToken()}`
  }
})
// 获取文章列表

export const articlesListAPI = ({ channel_id, timestamp }) => {
  return request({
    url: '/v1_0/articles',
    headers: {
      Authorization: `Bearer ${getToken()}`
    },
    params: {
      channel_id,
      timestamp
    }
  })
}

// 文章反馈不感兴趣
export const dislikeArticleAPI = ({ target }) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  headers: {
    Authorization: `Bearer ${getToken()}`
  },
  data: {
    target
  }
})
// 文章反馈垃圾内容

export const reportArticleAPI = ({ artId, type }) => request({
  url: '/v1_0/article/reports',
  method: 'POST',
  headers: {
    Authorization: `Bearer ${getToken()}`
  },
  data: {
    target: artId,
    type: type,
    remark: ''

  }
})

// 更换覆盖频道

export const updateChannelsAPI = ({ channels }) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels
  }
})
// 删除指定用户频道

export const removeTheChannelAPI = ({ channelId }) => request({
  url: `/v1_0/user/channels/${channelId}`,
  method: 'DELETE'
})

// 搜索菜单列表
export const suggestListAPI = ({ keywords }) => request({
  url: '/v1_0/suggestion',
  method: 'GET',
  params: {
    q: keywords
  }
})

// 获取搜索结果
export const searchResultAPI = ({ q, page = 1 }) => request({
  url: '/v1_0/search',
  method: 'GET',
  params: {
    q,
    page
  }
})
// 获取新闻详情

export const newListAPI = ({ artid }) => request({
  url: `/v1_0/articles/${artid}`
})

// 关注作者

export const userFollowedAPI = ({ userId }) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: userId
  }
})

// 取关作者
export const userUnFollowedAPI = ({ userId }) => request({
  url: `/v1_0/user/followings/${userId}`,
  method: 'DELETE'
})
// 喜欢点赞

export const likeArtcleApI = ({ artId }) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: artId
  }
})

// 不喜欢取消点赞
export const unLikeArticleAPI = ({ artId }) => request({
  url: `/v1_0/article/likings/${artId}`,
  method: 'DELETE'
})

// 文章评论获取

export const commentList = ({ id, offset = null, limit = 10 }) => request({
  url: '/v1_0/comments',
  params: {
    type: 'a',
    source: id,
    offset,
    limit
  }
})
// 对评论不喜欢

export const unLovecomment = ({ target }) => request({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target
  }
})

//  对评理喜欢

export const Lovecomment = ({ target }) => request({
  url: `/v1_0/comment/likings/${target}`,
  method: 'DELETE'
})

// 对文章或者评论进行评论
export const commentSendAPI = ({ target, content, art_id = null }) => {
  const obj = {
    target,
    content
  }
  if (art_id !== null) {
    obj.art_id = art_id
  }
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}
//  获取用户自己信息

export const getUserInfoAPI = () => request({
  url: '/v1_0/user'
})

export const getProfileAPI = () => request({
  url: '/v1_0/user/profile'
})

// 编辑用户个人资料
export const updateUserPhotoAPI = (fd) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  data: fd
})
// 更新用户个人资料

export const updateUserProfileAPI = (dataObj) => {
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: dataObj
  })
}
// 更新 生日

export const fromatDate = (dateObj) => {
  return dayjs(dateObj).format('YYYY-MM-DD')
}

// 用户- 刷新token
export const getNewTokenAPI = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    // 请求拦截器统一携带的是token,而这次需要带的是refresh_token
    // 所以在axios请求拦截器里判断,就是为了这种情况准备的
    Authorization: 'Bearer ' + getStorage('refresh_token')
  }
})
